#ifndef SERVER_H
#define SERVER_H

#include <QWidget>
#include "Resignature.h"
#include "Vacation.h"
#include "Manage.h"
#include "Face.h"

#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QTcpServer>
#include <QTcpSocket>

#include <QTimer>
#include <QDate>
#include <QString>
#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui {
class Server;
}
QT_END_NAMESPACE

class Server : public QWidget
{
    Q_OBJECT

public:
    Server(QWidget *parent = nullptr);
    ~Server();

    void checkAttendanceGroups();  // 检查考勤组并生成考勤表

protected:
    // 用于拖动窗口的事件处理函数
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;

    void paintEvent(QPaintEvent *event) override;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void onNewConnection();  // 处理新的连接
    void onReadyRead();      // 处理接收到的数据

    void onNewConnection1();
    void onReadyRead1();

    void onNewConnection2();
    void onReadyRead2();

    void onNewConnection3();
    void onReadyRead3();

    void onNewConnection4();
    void onReadyRead4();

    void onNewConnection5();
    void onReadyRead5();

    void onNewConnection6();
    void onReadyRead6();

    void onNewConnection7();
    void onReadyRead7();

    void onNewConnection8();
    void onReadyRead8();

    void onNewConnection9();
    void onReadyRead9();

    void onNewConnection10();
    void onReadyRead10();

    void onDayChanged();  // 日期变化时的操作

private:
    Ui::Server *ui;
    QSqlDatabase db;
    void initializeDatabase(); // 初始化数据库
    void createTables();       // 创建表

    QStringList fetchTableData(const QString &employeeId, const QString &tableName);

    QTcpServer *tcpServer;
    QTcpServer *tcpServer1;
    QTcpServer *tcpServer2;
    QTcpServer *tcpServer3;
    QTcpServer *tcpServer4;
    QTcpServer *tcpServer5;
    QTcpServer *tcpServer6;
    QTcpServer *tcpServer7;
    QTcpServer *tcpServer8;
    QTcpServer *tcpServer9;
    QTcpServer *tcpServer10;
    void handleClientRequest(QTcpSocket *clientSocket, const QString &request);  // 处理客户端请求
    void handleClientRequest1(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest2(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest3(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest4(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest5(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest6(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest7(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest8(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest9(QTcpSocket *clientSocket, const QString &request);
    void handleClientRequest10(QTcpSocket *clientSocket, const QString &request);

    void processPreviousDay();     // 结算前日考勤数据
    QStringList parseAttendanceDays(const QString &attendanceDays);  // 解析考勤日期

    QTimer *timer;  // 用于检测日期变化
    QDate currentDate;  // 当前日期，用于检查是否跨天

    void createAttendanceTable(const QString &groupId, const QDate &date); // 创建考勤表
    void finalizePreviousDay(const QString &groupId, const QDate &prevDate); // 结算前日表
    void updateEmployeeAttendanceSummary(const QString &employeeId, const QString &groupId, const QDate &date, bool isOnLeave);

    QString getEmployeeName(const QString &employeeId);
    QStringList getAttendanceGroupIds(const QString &employeeId);
    QStringList getAbsenteeDates(const QString &tableName, const QString &groupId);

    bool insertLeaveApplication(const QString &employeeId, const QString &employeeName, const QString &groupId, const QString &startDate, const QString &endDate, const QString &reason);

    // 用于记录拖动时鼠标位置的成员变量
    QPoint m_dragPosition;

    // 添加用于判断是否点击标题栏的成员变量
    bool m_isTitleBarClicked = false;
};
#endif // SERVER_H
